<template>
  <div>
    <div style="margin-bottom: 10px; font-size: 14px; font-weight: 250; margin-left: 10px">
      <el-icon><Finished /></el-icon>
      选课管理
    </div>
    <el-card style>
      <div style="display: flex">
        <el-button type="primary" size="large" @click="addStudentCourse">创建选课</el-button>
          <el-checkbox v-model="data.allowSelect" label="允许学生选课" size="large" style="margin-left: 20px" />
          <el-checkbox v-model="data.allowGrade" label="允许老师打分" size="large" />
        <el-input placeholder="请输入课程名称" v-model="data.courseName" style="width: 300px; margin-left: 38px;"  prefix-icon="Search"/>
        <el-input placeholder="请输入学生姓名" v-model="data.studentName" style="width: 300px; margin: 0px 20px 0px 20px" prefix-icon="Search"/>
        <el-input placeholder="请输入班级名称" v-model="data.className" style="width: 300px; margin: 0px 20px 0px 0px" prefix-icon="Search"/>
        <el-button type="primary" size="large" @click="load">搜索</el-button>
      </div>
      <el-table :data="data.tableData" stripe style="width: 100%">
        <el-table-column prop="id" label="选课ID" />
        <el-table-column prop="courseName" label="课程名" />
        <el-table-column prop="studentName" label="学生名" />
        <el-table-column prop="className" label="学生班级" />
        <el-table-column prop="dailyScore" label="日常分" />
        <el-table-column prop="examScore" label="期末分" />
        <el-table-column prop="score" label="总分" />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button @click="updateStudentCourse(scope.row)" type="primary" icon="Edit" circle />
            <el-button @click="handleDel(scope.row.id)" type="danger" icon="Delete" circle />
          </template>
        </el-table-column>
      </el-table>
      <div style="display: flex; justify-content: center; margin-top: 10px">
        <el-pagination layout="prev, pager, next" :total="data.tableData.length" :page-size="10" />
      </div>
    </el-card>
    <el-dialog v-model="data.dialogFormVisible" title="编辑" width="500">
      <el-form ref="formRef" :rules="data.rules" :model="data.form" label-width="80px" style="padding: 20px">
        <el-form-item label="学生" prop="student">
          <el-select v-model="data.form.studentId">
            <el-option
                v-for="(item, index) in data.students"
                :key="index"
                :label="item.name"
                :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="课程" prop="course">
          <el-select v-model="data.form.courseId">
            <el-option
                v-for="(item, index) in data.courses"
                :key="index"
                :label="item.name"
                :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="日常得分" prop="dailyScore">
          <el-input size="large" v-model="data.form.dailyScore" placeholder="请输入日常得分"></el-input>
        </el-form-item>
        <el-form-item label="考试得分" prop="examScore">
          <el-input size="large" v-model="data.form.examScore" placeholder="请输入期末得分"></el-input>
        </el-form-item>
        <el-form-item label="总分" prop="score">
          <el-input size="large" v-model="data.form.score" placeholder="请输入总分"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div>
          <el-button @click="data.dialogFormVisible = false" size="large">取消</el-button>
          <el-button type="primary" @click="handleSave" size="large">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>

import {reactive, ref} from "vue";
import request from "@/utils/request.js";
import {ElMessage, ElMessageBox} from "element-plus";

const formRef = ref()

const data = reactive({
  tableData: [],
  allowSelect: "",
  allowGrade: "",
  courseName: "",
  studentName: "",
  className: "",
  students: [],
  dialogFormVisible: false,
  form: {},
  rules: {
    dailyScore: [
      {required: true, message: "请输入日常得分", trigger: "blur"}
    ],
    examScore: [
      {required: true, message: "请输入考试得分", trigger: "blur"}
    ],
    score: [
      {required: true, message: "请输入总分", trigger: "blur"}
    ]
  },
  isAdd: true,
})

const load = () => {
  request.get("admin/selectAllStudentCourse", {
    params: {
      courseName: data.courseName || '',
      studentName: data.studentName || '',
      className: data.className || '',
    }}).then(res => {
    data.tableData = res.data
  })
}

load()

const getStudents = () => {
  request.get("admin/selectAllStudent").then(res => {
    data.students = res.data
  })
}

getStudents()

const getCourses = () => {
  request.get("admin/selectAllCourse").then(res => {
    data.courses = res.data
  })
}

getCourses()

const addStudentCourse = () => {
  data.dialogFormVisible = true;
  data.form = {
    studentId: 1,
    courseId: 1,
  };
  data.isAdd = true
}

const handleAdd = () => {
  request.post("admin/addStudentCourse", data.form).then(res => {
    if (res.code === "200") {
      ElMessage.success("创建成功")
      load()
      data.dialogFormVisible = false
    } else {
      ElMessage.error(res.msg)
    }
  })
}

const updateStudentCourse = (row) => {
  request.get("admin/selectStudentCourse", {params: {id: row.id}}).then(res => {
    data.form = res.data
  })
  data.dialogFormVisible = true
  data.isAdd = false
}

const handleEdit = () => {
  request.put("admin/updateStudentCourse", data.form).then(res => {
    if (res.code === "200") {
      ElMessage.success("更新成功")
      load()
      data.dialogFormVisible = false
    } else {
      ElMessage.error(res.msg)
    }
  })
}

const handleSave = () => {
  formRef.value.validate((valid) => {
    if(valid) {
      data.isAdd ? handleAdd() : handleEdit()
    }
  })
}

const handleDel = (id) => {
  ElMessageBox.confirm(
      '删除后无法复原，是否确定删除',
      '删除提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  ).then(() => {
    request.delete("admin/deleteStudentCourse/" + id).then(res => {
      if(res.code === "200") {
        ElMessage.success("删除成功")
        load()
      } else {
        ElMessage.error(res.msg)
      }
    })
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '取消删除',
    })
  })
}
</script>